package com.lhkj.ct.admin.modules.questionnaire.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblQnr;
import com.lhkj.ct.meta.modules.gauge.service.QnrConfigService;
import com.lhkj.ct.meta.modules.psychology.model.dto.GaugeAllotDto;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAllotRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问卷分配记录 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-26
 */
@RestController
@RequestMapping("api/plat/qnr/allot")
@Api(tags = {"5.1问卷分配"})
@Validated
public class QnrAllotController {

    @Resource
    private QnrConfigService qnrConfigService;

    @Resource
    private QnrAllotRecordService qnrAllotRecordService;

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "问卷列表", notes = "按问卷分类进行分组")
    @GetMapping("qnrs")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<List<Map<String, List<Map<String, Object>>>>> selectPatUnitPage(TblQnr tblQnr) {
        return ActionResult.success(qnrConfigService.groupByCategory(tblQnr));
    }

    @ApiOperationSupport(order = 5)
    @PreventDuplicateSubmit
    @PostMapping(value = "allot")
    @ApiOperation(value = "问卷分配")
    @Log(title = "问卷分配", actionType = ActionType.INSERT)
//    //@RequiresPermissions("XLCP:XLCY:LBFP:MAIN")
    public ActionResult<Void> allot(@Validated({ValGroups.Insert.class, GaugeAllotDto.Qnr.class}) @RequestBody GaugeAllotDto allotDto) {
        Asserts.assertFalse(CollectionUtils.isEmpty(allotDto.getPatIds())
                && CollectionUtils.isEmpty(allotDto.getPatLabelIds())
                && CollectionUtils.isEmpty(allotDto.getUnitIds()), "请选择需要分配的对象");
        return ActionResult.judge(qnrAllotRecordService.allot(allotDto));
    }
}
